﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FreeDAM.Domain.Abstract;
using FreeDAM.Domain.Entities;
using System.Data.Entity;

namespace FreeDAM.WebUI.Areas.Admin.Controllers
{
    public class RenditionGroupsController : Controller
    {
        private readonly IRenditionGroupRepository RenditionGroupRepository;
        private readonly IRenditionDefinitionRepository RenditionDefinitionRepository;

        public RenditionGroupsController(IRenditionGroupRepository renditionGroupRepository, IRenditionDefinitionRepository renditionDefinitionRepository)
        {
            RenditionGroupRepository = renditionGroupRepository;
            RenditionDefinitionRepository = renditionDefinitionRepository;
        }

        public ActionResult Index()
        {
            return View(RenditionGroupRepository.All);
        }

        public ActionResult Create()
        {
            ViewBag.RenditionDefinitions = new MultiSelectList(RenditionDefinitionRepository.All.OrderBy(rd => rd.Name), "Id", "Name");
            return View();
        }

        [HttpPost]
        public ActionResult Create(RenditionGroup viewModel, int[] renditionDefinitionIds)
        {
            ActionResult result = View(viewModel);

            if (ModelState.IsValid)
            {
                viewModel.RenditionDefinitions = RenditionDefinitionRepository.All.Where(rd => renditionDefinitionIds.Contains(rd.Id)).ToList();
                RenditionGroupRepository.InsertOrUpdate(viewModel);
                RenditionGroupRepository.Save();
                result = RedirectToAction("Index");
            }
            else
            {
                ViewBag.RenditionDefinitions = new MultiSelectList(RenditionDefinitionRepository.All.OrderBy(rd => rd.Name), "Id", "Name");
            }

            return result;
        }

        public ActionResult Edit(int id)
        {
            RenditionGroup viewModel = RenditionGroupRepository.Find(id);
            ViewBag.RenditionDefinitions = new MultiSelectList(RenditionDefinitionRepository.All.OrderBy(rd => rd.Name), "Id", "Name", viewModel.RenditionDefinitions.Select(rd => rd.Id));

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Edit(RenditionGroup viewModel, int[] renditionDefinitionIds)
        {
            ActionResult result = View(viewModel);
            RenditionGroup renditionGroup;

            if (ModelState.IsValid)
            {
                renditionGroup = RenditionGroupRepository.Find(viewModel.Id);
                if (TryUpdateModel(renditionGroup))
                {
                    foreach (int renditionDefinitionId in renditionDefinitionIds)
                    {
                        if (renditionGroup.RenditionDefinitions.Count(rd => rd.Id == renditionDefinitionId) == 0)
                        {
                            renditionGroup.RenditionDefinitions.Add(RenditionDefinitionRepository.Find(renditionDefinitionId));
                        }
                    }
                    renditionGroup.RenditionDefinitions.RemoveAll(rd => !renditionDefinitionIds.Contains(rd.Id));

                    RenditionGroupRepository.InsertOrUpdate(renditionGroup);
                    RenditionGroupRepository.Save();
                    result = RedirectToAction("Index");
                }
                else
                {
                    ViewBag.RenditionDefinitions = new MultiSelectList(RenditionDefinitionRepository.All.OrderBy(rd => rd.Name), "Id", "Name", renditionDefinitionIds);
                }
            }
            else
            {
                ViewBag.RenditionDefinitions = new MultiSelectList(RenditionDefinitionRepository.All.OrderBy(rd => rd.Name), "Id", "Name", renditionDefinitionIds);
            }

            return result;
        }

        public ActionResult Details(int id)
        {
            return View(RenditionGroupRepository.Find(id));
        }
    }
}
